﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using System.Text;

using Continental.Code.BL;
using Continental.Code.Entity;

namespace Continental.Admin
{
    public partial class ManagerQuanHuyen : System.Web.UI.Page
    {
        public String message = "";
        public Int32 quanHuyenID = -1;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                try
                {
                    quanHuyenID = Int32.Parse(Request.QueryString["QuanHuyenID"].ToString());
                    ViewState["ID"] = quanHuyenID;
                    ViewState["ACTION"] = "UPDATE";
                    QuanHuyen theQuanHuyen = ExtensionProvider.GetQuanHuyenByID(quanHuyenID);
                    this.txtNumberOfOrder.Text = theQuanHuyen.NumberOfOrder.ToString();
                    this.txtTitleOrName.Text = theQuanHuyen.TitleOrName;
                    this.btnSave.Text = "Cập nhật";
                    this.btnDelete.Enabled = true;
                    this.ltlStatus.Text = "Bạn Đang Sửa : " + theQuanHuyen.TitleOrName;
                }
                catch
                {
                    ViewState["ID"] = null;
                    ViewState["ACTION"] = "ADD";
                    this.btnSave.Text = "Thêm mới";
                    this.btnDelete.Enabled = false;
                    this.ltlStatus.Text = "Thêm mới";
                }

                LoadQuanHuyen();
            }
        }

        private void LoadQuanHuyen()
        {
            DataSet dsResult = ExtensionProvider.GetQuanHuyenList();

            if (dsResult.Tables[0].Rows.Count > 0)
            {
                StringBuilder sb = new StringBuilder();
                sb.Append("<table class='QuanHuyenList'>");
                Int32 count = 1;
                foreach (DataRow dr in dsResult.Tables[0].Rows)
                {
                    if (count == 1)
                    {
                        sb.Append("<tr>");
                    }

                    sb.Append("<td>");
                    sb.Append("<a href='ManagerQuanHuyen.aspx?QuanHuyenID=" + dr["QuanHuyenID"].ToString() + "'>");
                    sb.Append("<H2>" + dr["TitleOrName"].ToString() + "</H2>");
                    sb.Append("</a>");
                    sb.Append("</td>");
                    count += 1;
                    if (count == 6)
                    {
                        count = 1;
                        sb.Append("</tr>");
                    }
                }
                sb.Append("</table>");
                this.ltlQuanHuyenList.Text = sb.ToString();
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            String action = ViewState["ACTION"].ToString();
            Page.Validate("CAPNHAT");
            if (Page.IsValid)
            {
                QuanHuyen theQuanHuyen = new QuanHuyen();
                theQuanHuyen.TitleOrName = this.txtTitleOrName.Text.Trim();
                try
                {
                    theQuanHuyen.NumberOfOrder = Int32.Parse(this.txtNumberOfOrder.Text.Trim());
                }
                catch
                {
                    theQuanHuyen.NumberOfOrder = 1;
                }

                if (action.Equals("ADD"))
                {
                    ExtensionProvider.InserQuanHuyen(theQuanHuyen);
                    message += "Thêm mới thành công";
                    Response.Redirect("ManagerQuanHuyen.aspx");
                }
                else if (action.Equals("UPDATE"))
                {
                    theQuanHuyen.QuanHuyenID = Int32.Parse(ViewState["ID"].ToString());
                    ExtensionProvider.UpdateQuanHuyenByID(theQuanHuyen);
                    message += "Cập nhật thành công";
                    Response.Redirect("ManagerQuanHuyen.aspx");
                }
            }
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            quanHuyenID = Int32.Parse(ViewState["ID"].ToString());
            ExtensionProvider.DeleteQuanHuyenByID(quanHuyenID);
            message += "Xóa thành công";
            Response.Redirect("ManagerQuanHuyen.aspx");
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            Response.Redirect("ManagerQuanHuyen.aspx");
        }
    }
}
